module.exports = app => {

  require('dotenv').config()
  const express = require('express')
  const jwt = require("jsonwebtoken")


  const router = express.Router({
    mergeParams: true
  })
  /*======================== 定义接口以及方法 start ========================*/
  let result = {
    code: 200,
    msg: "请求成功"
  }
  // 登陆
  router.post('/logIn', async (req, res) => {
    const logInInfo = {
      account: req.body.account,
      password: req.body.password
    }

    const model = await req.ModelData.find(logInInfo)

    if (model.length === 1) {
      const { createToken } = require("../public/javascripts/token");
      const user = model[0];
      const token = createToken({ user });
      res.json({ token: token })
    }
    else {
      res.send(false)
    }
  });

  // 验证token
  router.post("/verToken", (req, res) => {
    const { verToken } = require("../public/javascripts/token")
    verToken(req.body.token, res)
  })
  /*======================== 定义接口以及方法 end ========================*/

  const resourceMiddleWare = async (req, res, next) => {
    req.ModelData = await require(`../models/${req.params.resource}`) // 给请求绑定原型的名称并引入数据库原型,
    next()
  }
  app.use('/api/rest/:resource', resourceMiddleWare, router)
}